﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnllyUIController : MonoBehaviour {

    public static OnllyUIController _instance;
    public int i_pageNum;            //第几页（0表示第一页）
    public Transform tf_allPage;
    public Transform tf_bottomUI;
    private Color32 c_redBG = new Color32(186,44,58,255);
    private Color32 c_blackBG = new Color32(55,55,81,255);
    private Color32 c_whiteText = new Color32(255,255,255,255);
    private Color32 c_redText = new Color32(200,38,81,255);
    private Color32 c_blackText = new Color32(15,31,38,255);
    private Color32 c_blueText = new Color32(63,82,159,255);
    private Color32 c_huiText = new Color32(15,31,38,70);
    public Light l_zhishideng;
    public GameObject go_yunxingdeng;
    public bool b_isDuihuakuang;           //是否出现的正式开始实验的对话框
    public int i_shiyancishu;              //试验的次数

    public GameObject go_dianyuandeng;
    public GameObject go_heiping;
    public bool b_isHeiPing=true;

    // Use this for initializatio
    void Start () {
        _instance = this;
        EventManager<string>.AddEvent(MyEventType.OnllyPageButtonClickEvent,OnPgButtonClick);
        EventManager<bool>.AddEvent(MyEventType.OnllyHeiPingEvent,ChangeHeiping);
    }

    private void ChangeHeiping(bool active)
    {
        b_isHeiPing = active;
        go_heiping.SetActive(active);
        go_dianyuandeng.SetActive(!active);
    }

    void Update() {
        tf_bottomUI.Find("data").GetChild(0).GetComponent<Text>().text = System.DateTime.Now.ToString("yyyy-MM-dd           HH:mm:ss");
    }

    /// <summary>
    /// 控制页面的跳转
    /// </summary>
    /// <param name="dir"></param>
    void OnPgButtonClick(string dir)
    {
        switch (dir)
        {
            case "pgup":
                if (i_pageNum < tf_allPage.childCount - 2 && !OnllyPage2UIController._instance.b_isStart && !OnllyPage2UIController._instance.b_isEnterClick)
                {
                    if (i_pageNum >= 4) {
                        tf_allPage.GetChild(i_pageNum).gameObject.SetActive(true);
                    }else
                        tf_allPage.GetChild(i_pageNum).gameObject.SetActive(false);
                    EventManager<bool>.TriggerEvent(MyEventType.OnllyReSetPageEvent, true);
                    ChangeBottomUI(i_pageNum, false);
                    i_pageNum++;
                    ChangeBottomUI(i_pageNum, true);
                    tf_allPage.GetChild(i_pageNum).gameObject.SetActive(true);
                }
                break;
            case "pgon":
                if (i_pageNum > 0 && !OnllyPage2UIController._instance.b_isStart && !OnllyPage2UIController._instance.b_isEnterClick)
                {
                    if (i_pageNum >= 4)
                    {
                        tf_allPage.GetChild(i_pageNum).gameObject.SetActive(true);
                    }
                    else
                        tf_allPage.GetChild(i_pageNum).gameObject.SetActive(false);
                    tf_allPage.GetChild(i_pageNum).gameObject.SetActive(false);
                    EventManager<bool>.TriggerEvent(MyEventType.OnllyReSetPageEvent, true);
                    ChangeBottomUI(i_pageNum, false);
                    i_pageNum--;
                    ChangeBottomUI(i_pageNum, true);
                    tf_allPage.GetChild(i_pageNum).gameObject.SetActive(true);
                }
                break;
            default: break;
        }
    }

    /// <summary>
    /// 改变小方框的背景颜色和字体的颜色
    /// </summary>
    /// <param name="type"></param>
    /// <param name="name"></param>
    public void ChangeType(Transform tf,int type, string name)
    {
        switch (type)
        {
            case 0://normal1
                tf.Find(name).GetChild(0).gameObject.SetActive(false);
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_blackText;
                break;
            case 1://normal2
                tf.Find(name).GetChild(0).gameObject.SetActive(false);
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_redText;
                break;
            case 2://normal3
                tf.Find(name).GetChild(0).gameObject.SetActive(false);
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_blueText;
                break;
            case 3://normal4
                tf.Find(name).GetChild(0).gameObject.SetActive(false);
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_huiText;
                break;
            case 4://hit1
                tf.Find(name).GetChild(0).gameObject.SetActive(true);
                tf.Find(name).GetChild(0).GetComponent<Image>().color = c_redBG;
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_whiteText;
                break;
            case 5://hit2
                tf.Find(name).GetChild(0).gameObject.SetActive(true);
                tf.Find(name).GetChild(0).GetComponent<Image>().color = c_blackBG;
                tf.Find(name).GetChild(1).GetComponent<Text>().color = c_whiteText;
                break;
            default:break;
        }
    }

    /// <summary>
    /// 改变底部UI
    /// </summary>
    /// <param name="index"></param>
    /// <param name="active"></param>
    public void ChangeBottomUI(int index,bool active) {
        if (!active)
        {
            tf_bottomUI.GetChild(index).GetChild(0).gameObject.SetActive(active);
            tf_bottomUI.GetChild(index).GetChild(1).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
        }
        else {
            tf_bottomUI.GetChild(index).GetChild(0).gameObject.SetActive(active);
            tf_bottomUI.GetChild(index).GetChild(1).GetComponent<Text>().color = Color.white;
        }
    }

    /// <summary>
    /// 改变Onlly指示灯的状态
    /// </summary>
    /// <param name="active">是否开始试验</param>
    /// <param name="time">延迟多少时间熄灭</param>
    public void ChangeZhishideng(bool active, float time)
    {
        if (active)//开始试验后
        {
            go_yunxingdeng.GetComponent<MeshRenderer>().material.color = Color.red;
            TimerMngr.Instance.Create(() =>
            {
                go_yunxingdeng.GetComponent<MeshRenderer>().material.color = Color.white;
            }).Start(time);
        }
        else
        {//每次按下按钮，指示灯亮绿time时间后熄灭
            go_yunxingdeng.GetComponent<MeshRenderer>().material.color = Color.green;
            TimerMngr.Instance.Create(() =>
            {
                go_yunxingdeng.GetComponent<MeshRenderer>().material.color = Color.white;
            }).Start(time);
        }
    }
    /// <summary>
    /// 重置
    /// </summary>
    void OnReset() {

    }
}
